﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.Product.Bll;

namespace CIT.WMS.Product
{
    public partial class FrmProductInByTorr : Form
    {
        public FrmProductInByTorr()
        {
            InitializeComponent();
        }

        private void cbx_Doc_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        public void InputBillNo()
        {
            DataRow[] drIn = DtIn.Select(string.Format(" BillNo = '{0}'", cbx_Doc.Text.ToString()));
            if (drIn.Length > 0)
            {

                DataTable info = Bll_ProductIn.GetInStockInfo(cbx_Doc.Text);
                if (info.Rows.Count > 0)
                {
                    if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                    {
                        lab_StorageSN.Text = info.Rows[0]["Storage_SN"].ToString();
                        BindAreaSN(lab_StorageSN.Text);
                        cbx_AreaSN.Focus();
                        lab_msg.Text = GetBillInfoMsg(cbx_Doc.Text);
                        txt_SNNum.Text = "1";
                    }
                    else
                    {
                        MessageBox.Show(cbx_Doc.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");
                        lab_StorageSN.Text = "";
                        cbx_Doc.Text = "";
                        cbx_Doc.Focus();
                    }
                }
            }
            else
            {
                //lab_msg.Text = cbx_Doc.Text + "单号不正确,请输入正确的单据!";
                MessageBox.Show(cbx_Doc.Text + "单号不正确,请输入正确的单据!");
                lab_StorageSN.Text = "";
                cbx_Doc.Focus();
                cbx_Doc.Text = "";
            }
        }

        private void cbx_Doc_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
                txt_SNNum.Text = "1";
            }
        }

        DataTable DtIn;

        public void BindCBX()
        {
            DtIn = Bll.Bll_ProductIn.GetInStockProductByTorr();
            cbx_Doc.Text = "";
            cbx_Doc.Items.Clear();

            if (DtIn.Rows.Count > 0)
            {
                foreach (DataRow dr in DtIn.Rows)
                {
                    cbx_Doc.Items.Add(dr["BillNo"].ToString());
                }
            }

        }

        DataTable DtAreaSN;

        public void BindAreaSN(string storageSN)
        {
            DtAreaSN = Bll_ProductIn.GetAreaSN(storageSN);
            cbx_AreaSN.Text = "";
            cbx_AreaSN.Items.Clear();

            if (DtAreaSN.Rows.Count > 0)
            {
                foreach (DataRow dr in DtAreaSN.Rows)
                {
                    cbx_AreaSN.Items.Add(dr["Area_SN"].ToString());
                }
            }
        }

        public void BindStandardType(string Area_SN)
        {
            var dt = Bll_ProductIn.GetStandardType(Area_SN);

            cbx_SNType.Text = "";
            cbx_SNType.Items.Clear();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    cbx_SNType.Items.Add(dr["StandardSN"].ToString());
                }
            }

        }

        private void FrmProductInByTorr_Load(object sender, EventArgs e)
        {
            //BindStandardType();
            BindCBX();
        }

        public string GetBillInfoMsg(string billNo)
        {
            var dt = Bll_ProductIn.GetStorageConfirmInfo(billNo);
            var result = string.Empty;

            var billType = string.Empty;
            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();

                billType = dt.Rows[0]["BillType"].ToString();
                var isFinish = false;
                foreach (DataRow dr in dt.Rows)
                {
                    var planQty = 0;
                    if (!string.IsNullOrEmpty(dr["PlanQty"].ToString()))
                    {
                        planQty = Convert.ToInt32(dr["PlanQty"]);
                    }
                    var realQty = 0;
                    if (!string.IsNullOrEmpty(dr["RealQty"].ToString()))
                    {
                        realQty = Convert.ToInt32(dr["RealQty"]);
                    }
                    sb.AppendLine("料号:" + dr["PartNumber"] + "\r\n收货数/计划数:" + realQty + "/" + planQty);

                    if (planQty <= realQty)
                    {
                        isFinish = true;
                    }
                }
                result = sb.ToString();
                if (isFinish)
                {
                    MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                    DialogResult dr = MessageBox.Show("入库单:" + billNo + "收货数量已满，是否确认完成?", "入库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                    if (dr == DialogResult.OK)
                    {
                        Bll_ProductIn.InStockFinish(billNo, billType);
                        BindCBX();
                        lab_StorageSN.Text = "";
                        cbx_Doc.Focus();
                        cbx_Doc.Text = "";
                        cbx_SNType.Text = "";
                        txt_SNNum.Text = "";
                    }
                }
            }
            return result;
        }

        private void cbx_SNType_SelectedIndexChanged(object sender, EventArgs e)
        {
            txt_SNNum.Focus();
        }

        private void txt_SNNum_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (string.IsNullOrEmpty(cbx_Doc.Text))
                {
                    MessageBox.Show("请先选择单号!");
                    cbx_Doc.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(cbx_AreaSN.Text))
                {
                    MessageBox.Show("请选择库区!");
                    cbx_AreaSN.Focus();
                    return;
                }

                if (string.IsNullOrEmpty(cbx_SNType.Text))
                {
                    MessageBox.Show("请选择库位类型!");
                    cbx_SNType.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(txt_SNNum.Text))
                {
                    //lab_msg.Text = "请输入库位数量!";
                    MessageBox.Show("请输入库位数量!");
                    txt_SNNum.Focus();
                    return;
                }

                try
                {
                    var snNum = 0;
                    snNum = Convert.ToInt32(txt_SNNum.Text);

                    if (snNum <= 0)
                    {
                        //lab_msg.Text = "库位数量不能小于0!";
                        MessageBox.Show("库位数量不能小于0!");
                        txt_SNNum.Focus();
                        return;
                    }

                    var topList = Bll_ProductIn.GetStandardInfoListTop(lab_StorageSN.Text, cbx_AreaSN.Text, cbx_SNType.Text, snNum);

                    if (topList.Rows.Count < snNum)
                    {
                        MessageBox.Show(cbx_AreaSN.Text + "库区的" + cbx_SNType.Text + "类型的库位数为" + topList.Rows.Count + ";不足" + snNum + "，请重新选择库区");
                        cbx_AreaSN.Focus();
                        return;
                    }


                    var dt = Bll_ProductIn.GetStandardInfoList(lab_StorageSN.Text, cbx_SNType.Text);

                    if (dt.Rows.Count < snNum)
                    {
                        //lab_msg.Text = cbx_SNType.Text + "输入的库位数量大于仓库库位数，请重新输入!";
                        MessageBox.Show("库位数量" + snNum + "大于未占用的库位数量" + dt.Rows.Count + "!");
                        txt_SNNum.Focus();

                    }
                    else
                    {
                        FrmProductInByTorr2 fr = new FrmProductInByTorr2(cbx_Doc.Text, cbx_SNType.Text, lab_StorageSN.Text, cbx_AreaSN.Text, snNum);
                        fr.ShowDialog();

                        InputBillNo();

                    }

                }
                catch (Exception ex)
                {
                    //lab_msg.Text = "请输入正确的库位数量，只能为数字!";
                    MessageBox.Show("请输入正确的库位数量，只能为数字!");
                    txt_SNNum.Focus();
                }


            }
        }

        private void cbx_AreaSN_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindStandardType(cbx_AreaSN.Text);
            cbx_SNType.Focus();
        }

        private void cbx_AreaSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            string areaSN = cbx_AreaSN.Text;
            try
            {
                areaSN = areaSN.Split('#')[0] + '#' + areaSN.Split('#')[1];
                cbx_AreaSN.Text = areaSN;
            }
            catch (Exception ex)
            {
                MessageBox.Show("输入的库区" + areaSN + "格式不正确,请输入正确的库区!");
                cbx_AreaSN.Text = "";
                cbx_AreaSN.Focus();
                return;
            }

            DataRow[] drArea = DtAreaSN.Select(string.Format(" Area_SN = '{0}'", cbx_AreaSN.Text));
            if (drArea.Length > 0)
            {
                cbx_SNType.Focus();
            }
            else
            {
                //lab_msg.Text = cbx_Doc.Text + "单号不正确,请输入正确的单据!";
                MessageBox.Show(cbx_AreaSN.Text + "库区不正确,请输入正确的库区!");
                cbx_AreaSN.Text = "";
                cbx_AreaSN.Focus();
            }

        }

        private void cbx_SNType_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                txt_SNNum.Focus();
            }
        }


    }
}